package com.ruoyi.system.service.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.system.domain.GgPzDb;
import com.ruoyi.system.mapper.GgPzDbMapper;
import com.ruoyi.system.service.BaseServiceImpl;
import com.ruoyi.system.service.IGgPzDbService;

import lombok.extern.slf4j.Slf4j;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.response.ResponseData;
import start.spring.basic.util.common.StringUtil;

/**
 * 数据库配置Service业务层处理
 * 
 * @author xuyy
 * @date 2023-08-08
 */
@Slf4j
@Service
public class GgPzDbServiceImpl extends BaseServiceImpl implements IGgPzDbService {
    
    @Autowired
    private GgPzDbMapper ggPzDbMapper;

    /**
     * 查询数据库配置列表
     * 
     * @param ggPzDb 数据库配置
     * @return 数据库配置
     */
    @Override
    public PageResult<GgPzDb> selectGgPzDbPageList(PageQuery pageQuery,GgPzDb ggPzDb) {
        IPage<GgPzDb> page = super.getPage(pageQuery);
        QueryWrapper<GgPzDb> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(!StringUtil.isEmpty(ggPzDb.getDbid()), GgPzDb::getDbid, ggPzDb.getDbid());
    	queryWrapper.lambda().like(!StringUtil.isEmpty(ggPzDb.getUseFor()), GgPzDb::getUseFor, ggPzDb.getUseFor());
        page = this.ggPzDbMapper.selectPage(page, queryWrapper);
        return super.getPageResult(page);
    }
    
    /**
     * 查询全量数据库配置
     * 
     * @param ggPzDb 数据库配置
     * @return 数据库配置
     */
    @Override
    public List<GgPzDb> selectGgPzDbList(GgPzDb ggPzDb) {
        QueryWrapper<GgPzDb> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(!StringUtil.isEmpty(ggPzDb.getDbid()), GgPzDb::getDbid, ggPzDb.getDbid());
    	queryWrapper.lambda().eq(!StringUtil.isEmpty(ggPzDb.getUseFor()), GgPzDb::getUseFor, ggPzDb.getUseFor());
        return this.ggPzDbMapper.selectList(queryWrapper);
    }

    /**
     * 修改数据库配置
     * 
     * @param ggPzDb 数据库配置
     * @return 结果
     */
    @Override
    public int updateGgPzDb(GgPzDb ggPzDb) {
    	UpdateWrapper<GgPzDb> updateWrapper = new UpdateWrapper<>();
    	updateWrapper.lambda().eq(GgPzDb::getDbid, ggPzDb.getDbid());
        return this.ggPzDbMapper.update(ggPzDb, updateWrapper);
    }

	@Override
	public ResponseData<String> testGgPzDbByDbIds(List<String> dbIds) {
		String dbid = dbIds.get(0);
		QueryWrapper<GgPzDb> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(!StringUtil.isEmpty(dbid), GgPzDb::getDbid, dbid);
		GgPzDb ggPzDb = this.ggPzDbMapper.selectOne(queryWrapper);
		if(ggPzDb!=null) {
        	return testDbConfig(ggPzDb);
        }else {
        	return ResponseData.errorMsg("未查询到数据库配置信息，请传入正确的数据库唯一标志！");
        }
	}

	private ResponseData<String> testDbConfig(GgPzDb ggPzDb) {
		Connection conn = null;
		Statement st = null;
		try {
			Class.forName(ggPzDb.getDriverClassName());
			conn = DriverManager.getConnection(ggPzDb.getUrl(), ggPzDb.getUsername(), ggPzDb.getPwd());
			st = conn.createStatement();
			st.execute(ggPzDb.getValidationQuery());
			return ResponseData.successMsg("测试通过！");
		} catch (Exception e) {
			log.error("测试数据库配置出错！", e);
			return ResponseData.errorMsg("测试未通过，请检查配置是否正确，网络是否联通！");
		}finally {
			
			try {
				if(st!=null) {
					st.close();
				}
				
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				log.error("测试数据库配置完毕，关闭连接出错！", e);
			}
		}
	}
	
}
